package com.shengdong.water.code.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.core.graphics.Insets
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding
import com.shengdong.water.R


object StatusBarUtil {

    private var isFitsSystemWindowsEnabled = false
    /**
     * 设置状态栏颜色
     */
    fun setStatusBarColor(activity: Activity, color: Int) {
        val window = activity.window
        // 使用 WindowCompat 替代直接设置 statusBarColor

        if(!isFitsSystemWindowsEnabled) {
            WindowCompat.setDecorFitsSystemWindows(window, false)
        }
        val rootView = window.decorView.findViewById<View>(android.R.id.content)

        ViewCompat.setOnApplyWindowInsetsListener(rootView) { view, windowInsets ->
            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
            // 创建一个背景View来显示状态栏颜色
            if (!isFitsSystemWindowsEnabled) {
                ensureStatusBarColorView(activity, color, insets.top)
            }
            WindowInsetsCompat.Builder()
                .setInsets(WindowInsetsCompat.Type.systemBars(), insets)
                .build()
        }
    }

    private fun ensureStatusBarColorView(activity: Activity, color: Int, height: Int) {
        val decorView = activity.window.decorView as ViewGroup
        val statusBarId = R.id.status_bar_view

        // 查找或创建状态栏颜色视图
        var statusBarView = decorView.findViewById<View>(statusBarId)
        if (statusBarView == null) {
            statusBarView = View(activity).apply {
                id = statusBarId
                setBackgroundColor(color)
            }
            decorView.addView(statusBarView, ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                height
            ))
        } else {
            statusBarView.setBackgroundColor(color)
            statusBarView.updateLayoutParams {
                this.height = height
            }
        }
    }

    /**
     * 设置状态栏深色模式
     */
    fun setStatusBarDarkMode(activity: Activity, isDark: Boolean) {
        val window = activity.window
        val wic = WindowInsetsControllerCompat(window, window.decorView)
        wic.isAppearanceLightStatusBars = isDark
    }

    /**
     * 设置沉浸式状态栏
     */
    fun setImmersiveStatusBar(activity: Activity) {
        val window = activity.window
        WindowCompat.setDecorFitsSystemWindows(window, false)
        //fitsSystemWindows 控制
        isFitsSystemWindowsEnabled = false


        //处理刘海屏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            window.attributes.layoutInDisplayCutoutMode =
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
        }


        // 设置状态栏和导航栏为透明
        val wic = WindowInsetsControllerCompat(window, window.decorView)
        wic.isAppearanceLightStatusBars = true
        wic.isAppearanceLightNavigationBars = true
    }

    /**
     * 显示或隐藏状态栏
     */
    fun setStatusBarVisibility(activity: Activity, show: Boolean) {
        val wic = WindowInsetsControllerCompat(activity.window, activity.window.decorView)
        if (show) {
            wic.show(WindowInsetsCompat.Type.statusBars())
        } else {
            wic.hide(WindowInsetsCompat.Type.statusBars())
        }
    }

    //添加全局fitsSystemWindows控制
    fun setFitsSystemWindows(activity: Activity, enable: Boolean) {
        isFitsSystemWindowsEnabled = enable
        WindowCompat.setDecorFitsSystemWindows(activity.window, enable)
        ViewCompat.requestApplyInsets(activity.window.decorView)
    }


    /**
     * 获取状态栏高度
     */
    fun getStatusBarHeight(view: View): Int {
//        var statusBarHeight = 0
//        ViewCompat.setOnApplyWindowInsetsListener(view) { _, windowInsets ->
//            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
//            statusBarHeight = insets.top
//            windowInsets
//        }
//        return statusBarHeight
        val insets = ViewCompat.getRootWindowInsets(view)
            ?.getInsets(WindowInsetsCompat.Type.systemBars())
        return insets?.top ?: 0
    }

    /**
     * 为view添加状态栏高度的padding
     */
    fun addStatusBarPadding(view: View) {
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, windowInsets ->
            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
            // 仅在禁用fitsSystemWindows时添加padding
            if (!isFitsSystemWindowsEnabled) {
                v.updatePadding(
                    top = v.paddingTop + insets.top,
                    bottom = v.paddingBottom + insets.bottom
                )
            }
//            v.updatePadding(
//                top = v.paddingTop + insets.top
//            )
            windowInsets
        }
    }

    /**
     * 处理DrawerLayout等特殊布局
     */
    fun handleSpecialLayout(view: View) {
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, windowInsets ->
            val insets = windowInsets.getInsets(
                WindowInsetsCompat.Type.systemBars() or
                        WindowInsetsCompat.Type.displayCutout()
            )

            // 应用padding来处理系统栏和刘海屏
            v.updatePadding(
                left = insets.left,
                top = insets.top,
                right = insets.right,
                bottom = insets.bottom
            )

            // 返回消耗后的insets
            WindowInsetsCompat.Builder()
                .setInsets(WindowInsetsCompat.Type.systemBars(), Insets.NONE)
                .build()
        }
    }
}